package com.kuang.org.service;

import com.kuang.bascic.query.BaseQuery;
import com.kuang.bascic.service.BaseServiceImpl;
import com.kuang.bascic.utils.PageList;
import com.kuang.org.domain.Department;
import com.kuang.org.mapper.DepartmentMapper;
import com.kuang.org.query.DepartmentQuery;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.annotation.ApplicationScope;
import sun.dc.pr.PRError;

import java.util.*;

//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    // 新增和修改是特有的  不可抽离 抽离的代码只有基础新增修改
    @Transactional
    @Override
    public void addDepartment(Department department) {
        departmentMapper.addDepartment(department);
        // 将增加开启为主键自增 在xml里 useGeneratedKeys="true" keyColumn="id" keyProperty="id"
        this.UpdateDepartment(department);
    }
    @Transactional
    @Override
    public void UpdateDepartment(Department department) {
        //1 设置一个空的  dirPath
        String dirPath="";
        // 如果parent为空就是顶级部门 dirPath+id
        if (department.getParent()==null){
            dirPath="/"+department.getId();
        }else {
            // 不等于空找到父级部门获取父级部门的dirpath在加上自己的id
            // 如果parent不为空就是查找父级部门的dirPath加上自己的id
            Department department1 = departmentMapper.selectDepartmentById(department.getParent().getId());
            dirPath= department1.getDirPath()+"/"+department.getId();
        }
        // 放在最后 因为需要吧前面的设置完成才可以调用增加方法
        department.setDirPath(dirPath);
        departmentMapper.UpdateDepartment(department);

    }

    @Override
    public List<Department> selectTreeDepartment() {
        // 1查询出所有的部门
        List<Department> departments = departmentMapper.selectAllDepartment();
        // 思路： 将所有部门放进map key：为自己的id vulue：自己部门
        Map<Long, Department> depts = new HashMap<>();
        for (Department d : departments) {
            // 遍历全部部门 将所有部门放进map key：为自己的id vulue：自己部门
            depts.put(d.getId(), d);
        }
        // 2.查出所用的顶级部门  parent_id =null的部门
        List<Department> leve1List = new ArrayList<>();
        for (Department dept : departments) {
            if (dept.getParent_id() == null) {
                // 如果等于空说明这个部门是顶级部门 放入顶级部门的数组
                leve1List.add(dept);
                // 说明是子级部门 将自己加入父级部门
                // 这里根据集合的方法根据dept的parent_id去匹配集合的id查到的是当前的父部门
            } else {
                Department parent = depts.get(dept.getParent_id());
                if (parent != null) {
                    parent.getChildren().add(dept);

                }
            }
        }
        System.out.println(leve1List);
        return leve1List;

    }

}
